package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.atguigu.gmall.pms.entity.vo.AttrGroupVO;
import com.atguigu.gmall.pms.vo.ItemGroupVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 属性分组
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2023-02-09 10:16:38
 */
@Api(tags = "属性分组 管理")
@RestController
@RequestMapping("pms/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;
    @ApiOperation("根据分类id、spuId、skuId查询规格参数")
    @GetMapping("/withattrsGroup/{cid}/{spuId}/{skuId}")
    public ResponseVo< List<ItemGroupVO> > withattrsGroup(@PathVariable("cid")Long cid,
                                                          @PathVariable("spuId")Long spuId,
                                                          @PathVariable("skuId")Long skuId){
        List<ItemGroupVO>  attrGroups = attrGroupService.withattrsGroup(cid,spuId,skuId);
        return ResponseVo.ok(attrGroups);
    }
    ///withattrs/225
    @ApiOperation("根据选中的分类id查询spu属性组及下面的属性和值列表")
    @GetMapping("/withattrs/{categoryId}")
    public ResponseVo listGroupAndAttrsByCategoryId(@PathVariable("categoryId")Long categoryId){
        List<AttrGroupVO> attrGroupVOs = attrGroupService.listGroupAndAttrsByCategoryId(categoryId);
        return ResponseVo.ok(attrGroupVOs);
    }
    ////category/225
    @ApiOperation("根据分类id查询属性组")
    @GetMapping("/category/{cid}")
    public ResponseVo listByCategoryId(@PathVariable("cid") Long cid){
        return ResponseVo.ok(attrGroupService.list(Wrappers.lambdaQuery(AttrGroupEntity.class)
            .eq(AttrGroupEntity::getCategoryId ,cid)));
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrGroupByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = attrGroupService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrGroupEntity> queryAttrGroupById(@PathVariable("id") Long id){
		AttrGroupEntity attrGroup = attrGroupService.getById(id);

        return ResponseVo.ok(attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		attrGroupService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
